package com.topboya.linker.ui.presenter;

import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.model.response.CustomerByIdResponse;
import com.topboya.linker.model.response.DictByKeyResponse;
import com.topboya.linker.model.response.SelectFileResponse;
import com.topboya.linker.model.response.TitleDictResopnse;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.SeeSpecificView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SeeSpecificPresenter extends BasePresenter<SeeSpecificView> {
    private static final String TAG = "SeeSpecificPresenter";
    public SeeSpecificPresenter(BaseActivity context) {
        super(context);
    }

    public void getCustomerById(String id){

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getCustomerById(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(customerByIdResponse -> {
                    String success = customerByIdResponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();

                    if (null != success && Boolean.parseBoolean(success)) {

                        CustomerByIdResponse.Data data = customerByIdResponse.getData();
                        getView().getCustomerById( data);
                    }

                }, this::loginError);
    }


    public void getDictByKey(String type) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getDictByKey(type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(dictByKeyResponse -> {
                    LogUtils.v(TAG + " dictByKeyResponse:" + dictByKeyResponse.toString());
                    String success = dictByKeyResponse.getMeta().getSuccess();
                    if (null != success && Boolean.parseBoolean(success)) {
                        List<DictByKeyResponse.Data> listDepartment = dictByKeyResponse.getData();
                        LogUtils.v(TAG + listDepartment.toString());

                        getTitleByKey("Title", listDepartment);
                    }
                }, this::loginError);
    }

    public void getTitleByKey(String type, List<DictByKeyResponse.Data> listDepartment) {

        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getTitleByKey(type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(titledictresopnse -> {
                    mContext.hideWaitingDialog();
                    LogUtils.v(TAG + " titledictresopnse:" + titledictresopnse.toString());
                    String success = titledictresopnse.getMeta().getSuccess();
                    if (null != success && Boolean.parseBoolean(success)) {
                        List<TitleDictResopnse.Data> listTitle = titledictresopnse.getData();
                        LogUtils.v(TAG + listTitle.toString());
                        getView().setPickViewData(listDepartment, listTitle);
                    }

                }, this::loginError);
    }


    private void loginError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "loginError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }
}
